package ru.resumeService.web.servlet;

import ru.resumeService.model.Resume;
import ru.resumeService.service.ResumeService;
import ru.resumeService.service.impl.ResumeServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;

public class Edit extends HttpServlet {

    ResumeService resumeService = new ResumeServiceImpl();

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        resumeService = new ResumeServiceImpl();
        int id = Integer.parseInt(request.getParameter("id"));
        Resume resume = resumeService.getResumeById(id);
        try {
            List<String> headers = resumeService.getHeaders();
            if (headers != null) {
                request.setAttribute("headers", headers);
            }
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        request.setAttribute("resume", resume);
        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/edit.jsp");
        if (dispatcher != null) {
            try {
                dispatcher.forward(request, response);
            } catch (ServletException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");

        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        java.sql.Date sqlDate = null;
        try {
            date = dateFormat.parse(request.getParameter("date"));
            sqlDate = new java.sql.Date(date.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String department = request.getParameter("department");
        String city = request.getParameter("city");
        int age = Integer.parseInt(request.getParameter("age"));
        String education = request.getParameter("education");
        int experience = Integer.parseInt(request.getParameter("experience"));
        Double salary = Double.parseDouble(request.getParameter("salary"));
        String skills = request.getParameter("skills");
        int status;
        if (request.getParameter("status").equals("работает")) {
            status = 1;
        } else {
            status = 0;
        }
        String comments = request.getParameter("comments");
        //обновляем резюме по полученным значениям
        try {
            resumeService.updateResume(id,name,phone,email,sqlDate,department,city,age,education,experience,salary,skills,status,comments);
            request.setAttribute("successUpdate", "Резюме обновлено успешно!");
            Resume resume = resumeService.getResumeById(id);
            List<String> headers = resumeService.getHeaders();
            if (headers != null) {
                request.setAttribute("headers", headers);
            }
            request.setAttribute("resume", resume);
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/edit.jsp");
        if (dispatcher != null) {
            try {
                dispatcher.forward(request, response);
            } catch (ServletException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }

    }

}
